#include "gamecoin.h"

GameCoin::GameCoin(QString img,QWidget* parent)
    :QPushButton(parent)
    ,m_minIndex(1)
    ,m_maxIndex(8)
{
       if(img != ""){
           QPixmap normalPix;
           bool ret = normalPix.load(img);
           if(ret){
               QSize pSize(normalPix.width(),normalPix.height());
               this->setFixedSize(pSize);
               this->setStyleSheet("QPushButton{border:0px}");
               this->setIcon(normalPix);
               this->setIconSize(pSize);
           }
       }
       //定时器
       m_timerToFront = new QTimer(this);
       m_timerToBack = new QTimer(this);
       //转向金币
       connect(m_timerToFront,&QTimer::timeout,this,[=](){
           QString imgCoinStr = QString(":/res/Coin000%1.png").arg(--m_maxIndex);
           QPixmap imgPix;
           bool retImg = imgPix.load(imgCoinStr);
           if(retImg){
               QSize pSize(imgPix.width(),imgPix.height());
               this->setFixedSize(pSize);
               this->setStyleSheet("QPushButton{border:0px}");
               this->setIcon(imgPix);
               this->setIconSize(pSize);
           }
           if(m_minIndex > m_maxIndex){
               m_maxIndex = 8;
               m_timerToFront->stop();
           }
       });
       //转向银币
       connect(m_timerToBack,&QTimer::timeout,this,[=](){
           QString imgCoinStr = QString(":/res/Coin000%1.png").arg(++m_minIndex);
           QPixmap imgPix;
           bool retImg = imgPix.load(imgCoinStr);
           if(retImg){
               QSize pSize(imgPix.width(),imgPix.height());
               this->setFixedSize(pSize);
               this->setStyleSheet("QPushButton{border:0px}");
               this->setIcon(imgPix);
               this->setIconSize(pSize);
           }
           if(m_maxIndex < m_minIndex){
               m_minIndex = 1;
               m_timerToBack->stop();
           }
       });
}


void GameCoin::changeFlag()
{
    if(m_flag == true){
        m_timerToBack->start(30);
        m_flag = false;
    } else{
        m_timerToFront->start(30);
        m_flag = true;
    }
}

bool GameCoin::isWin() const
{
    return m_isWin;
}

void GameCoin::setIsWin(bool newIsWin)
{
    m_isWin = newIsWin;
}

bool GameCoin::isAnimation() const
{
    return m_isAnimation;
}

void GameCoin::setIsAnimation(bool newIsAnimation)
{
    m_isAnimation = newIsAnimation;
}
int GameCoin::posX() const
{
    return m_posX;
}

void GameCoin::setPosX(int newPosX)
{
    m_posX = newPosX;
}

int GameCoin::posY() const
{
    return m_posY;
}

void GameCoin::setPosY(int newPosY)
{
    m_posY = newPosY;
}

bool GameCoin::flag() const
{
    return m_flag;
}

void GameCoin::setFlag(bool newFlag)
{
    m_flag = newFlag;
}


